/*
 * @Author: 李超越 1509572300@qq.com
 * @Date: 2024-05-10 12:56:27
 * @LastEditors: 李超越 1509572300@qq.com
 * @LastEditTime: 2024-05-10 18:46:33
 * @FilePath: /undefined/root/my_git/操作系统/pid.c
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <signal.h>

/*
int main()
{
int p1,p2;
	if(p1=fork()) 
    {              
    	printf("b");
    }
	else
	{ 
		if(p2=fork())
        {              
  		    printf("c");
        }
    	else 
        {
            printf("a");
        }          
	}
}
*/


/* int main()
{
	int p1,p2,i;
	if(p1=fork())
    {
        for(i=0;i<500;i++)
        {
            printf("parent%d\n",i);
        }
        wait(0); 
        exit(0);
    }
	else
	{ 
        if(p2=fork())
        {
            for(i=0;i<500;i++)
            { 
                printf("son %d\n",i);
            }
            wait(0); 
            exit(0); 
        }
        else
        {
            for(i=0;i<500;i++)
            {  
                printf("grandchild %d\n",i); 
            }
            exit(0);
        }
	}
} */

/* int main()
{
	int p1,p2,i;
	if(p1=fork())
	{
     	lockf(1,1,0);
     	for(i=0;i<500;i++)  
        {
            printf("parent %d\n",i);
        }
     	lockf(1,0,0);
	    wait(0); 
	    exit(0);
    }
	else
 	{
  		if(p2=fork())
		{ 
			lockf(1,1,0);
     		for(i=0;i<500;i++)
            { 
                printf("son %d\n",i);
            }
    		lockf(1,0,0);
            wait(0); 
			exit(0);
  		}
		else
		{ 
		    lockf(1,1,0);
     		for(i=0;i<500;i++) 
            {
                printf("daughter %d\n",i);
            }
   	   	    lockf(1,0,0);
   	        exit(0);
		}
	}
}
*/

int wait_mark;

void waiting(){
    sleep(5);//休眠
    if(wait_mark != 0) kill(getpid(), SIGALRM);//向当前进程发出中断信号
}
void alarming(){
    wait_mark = 0;
}
void stop(){
    wait_mark = 0;
}

void main(){
    int pid1, pid2;
    while((pid1 = fork()) == -1);//创建子进程1
    if(pid1 > 0 ){//如果是父进程
        while((pid2 = fork()) == -1);//创建子进程2
        if(pid2 > 0)
		{//如果是父进程
            wait_mark = 1;//等待状态为1，等待中断信号
            signal(SIGINT,stop);//设置收到信号ctrl+C时，执行stop函数
            signal(SIGALRM,alarming);//设置收到SIGALRM（时钟）信号，则执行alarming函数
            waiting();//休眠5秒

            kill(pid1, 16);//向进程1发出信号16
            kill(pid2, 17);//向进程2发出信号17

            wait(0);//等待进程执行结束，同步
            wait(0);//等待进程执行结束，同步
            printf("parent precess is killed!\n");
            exit(0);//退出进程
        }
        else
		{
            wait_mark = 1;//等待中断信号
            signal(17,stop);//设置收到中断信号17，执行stop函数
            //signal(SIGINT,SIG_IGN);//设置收到ctrl+c时，忽略该信号
            while(wait_mark != 0);
            lockf(1,1,0);//加锁
            printf("child process2 is killed by parent!\n");
            lockf(1,0,0);//解锁
            exit(0);
        }
    }
    else{
        wait_mark = 1;
        signal(16,stop);//设置收到中断信号16，执行stop函数
        //signal(SIGINT,SIG_IGN);设置收到ctrl+c时，忽略该信号
        while(wait_mark != 0);
        lockf(1,1,0);//加锁
        printf("child process1 is killed by parent!\n");
        lockf(1,0,0);//解锁
        exit(0);
    }
}

